package com.esp.commentprovider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.esp.commentprovider.dao.NewsCommentMapper;
import com.esp.commentprovider.entity.NewsComment;
import com.esp.commentprovider.service.CommentService;
import com.esp.commentprovider.util.PageQueryUtil;
import com.esp.commentprovider.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<NewsCommentMapper, NewsComment> implements CommentService {
    @Autowired
    private NewsCommentMapper newsCommentMapper;

    @Override
    public List<NewsComment> selectNewsCommentLast(int limit) {
        QueryWrapper<NewsComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("comment_id").last("limit " + limit);
        return newsCommentMapper.selectList(queryWrapper);
    }

    @Override
    public Boolean addComment(NewsComment newsComment) {
        return newsCommentMapper.insertSelective(newsComment) > 0;
    }

    @Override
    public PageResult getCommentsPage(PageQueryUtil pageUtil) {
        List<NewsComment> comments = newsCommentMapper.findNewsCommentList(pageUtil);
        int total = newsCommentMapper.getTotalNewsComments(pageUtil);
        PageResult pageResult = new PageResult(comments, total, pageUtil.getLimit(), pageUtil.getPage());
        return pageResult;
    }

    @Override
    public Boolean checkDone(Integer[] ids) {
        return newsCommentMapper.checkDone(ids) > 0;
    }

    @Override
    public Boolean deleteBatch(Integer[] ids) {
        return newsCommentMapper.deleteBatch(ids) > 0;
    }
}
